﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using NPOI.XWPF.UserModel;

namespace FillWordDataByTemplate
{
    /// <summary>
    /// npoi导出word
    /// </summary>
    public class DocxHelper
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="model">实体数据</param>
        /// <param name="filepath">模板路径</param>
        /// <returns></returns>
        public static MemoryStream ExportDoc<T>(T model, string filepath)
        {
            using (FileStream stream = File.OpenRead(filepath))
            {
                XWPFDocument doc = new XWPFDocument(stream);
                //遍历段落
                foreach (var para in doc.Paragraphs)
                {
                    ReplaceKey(model, para);
                }
                //遍历表格
                var tables = doc.Tables;
                foreach (var table in tables)
                {
                    foreach (var row in table.Rows)
                    {
                        foreach (var cell in row.GetTableCells())
                        {
                            foreach (var para in cell.Paragraphs)
                            {
                                ReplaceKey(model, para);
                            }
                        }
                    }
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    doc.Write(ms);
                    return ms;
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="para"></param>
        private static void ReplaceKey<T>(T entity, XWPFParagraph para)
        {

            try
            {
                Type entityType = typeof(T);
                PropertyInfo[] properties = entityType.GetProperties();
                string entityName = entityType.Name;
                string paratext = para.ParagraphText;
                var runs = para.Runs;
                string styleid = para.Style;
                string text = "";
                foreach (var run in runs)
                {
                    text = run.ToString();
                    foreach (var p in properties)
                    {
                        string propteryName = "$" + p.Name + "$";
                        object value = p.GetValue(entity);
                        if (value == null)
                        {
                            value = "";
                        }
                        if (text.Contains(propteryName))
                        {
                            text = text.Replace(propteryName, value.ToString());
                        }
                        run.SetText(text);
                    }

                }
            }
            catch (Exception ex)
            {

                string msg = ex.Message + ex.StackTrace;
            }

        }

    }
}
